<?php

namespace App\Modules\User;

use App\Models\User\UsersInfo;
use App\Modules\AdapterApi;
use App\Services\Authcode\AuthcodeIndex;
use App\Services\Fund\FundIndex;
use App\Services\User\UserIndex;


/**
 * Class User 用户 应用层
 * @package App\Modules\User
 */
class User extends AdapterApi
{

    /**
     * 用户登录
     * @return array
     */
    public function login(): array
    {
        // 接收错误次数和检验图形验证码参数
        $data = pick_array($this->postData, ['name', 'login_password', 'token', 'img_type', 'img_code_id', 'auth_code', 'is_auto_login', 'token']);
        // 获取token内容
        $tokenContent = UserIndex::Token()->hGetAll($data['token']);
        if ($tokenContent['status'] != SUCCESS_STATUS) {
            return $tokenContent;
        }

        if ($tokenContent['res']['no_user_err_counts'] >= MAX_LOGIN_ERR_TIMES) {
            $checkAuthCode = $this->_checkAuthCode($data);
            if ($checkAuthCode['status'] != SUCCESS_STATUS) {
                return verify_err($tokenContent['res'], '验证图形验证码失败');
            }
        }

        //根据name获取用户信息中的login_err_times,用于判断是否检验图形验证码
        $baseInfo = UserIndex::User()->info(['name'=>$data['name'],'is_add_no_user_counts'=>1,'token'=>$data['token']]);
        if($baseInfo['status'] != SUCCESS_STATUS){
            $tokenContent['res']['no_user_err_counts'] = $tokenContent['res']['no_user_err_counts'] + 1;
            return verify_err($tokenContent['res'], '没有相关用户信息');
        }

        //判断是否为管理员登录
        if($baseInfo['res']['id'] > MAX_SYSTEM_ID){
            return verify_err(null, '非管理员不能进行登录后台');
        }
        //判断：若次数大于所允许的错误登录次数：验证图形验证码
//        if($baseInfo['res']['login_err_times'] >= MAX_LOGIN_ERR_TIMES && !isset($checkAuthCode)){
//            $checkAuthCode = $this->_checkAuthCode($data);
//            if($checkAuthCode['status'] != SUCCESS_STATUS){
//                $checkAuthCode['res']['last_err_login_time'] = $baseInfo['res']['last_err_login_time'];
//                $checkAuthCode['res']['login_err_times'] = $baseInfo['res']['login_err_times'];
//                $checkAuthCode['res']['max_login_err_times'] = MAX_LOGIN_ERR_TIMES;
//                $checkAuthCode['res']['no_user_err_counts'] = $tokenContent['res']['no_user_err_counts'];
//                return $checkAuthCode;
//            }
//        }

        //登录并获取用户信息 若第一次打勾自动登录 返回auto_login_token信息
        $loginInfoRes = UserIndex::User()->login($data);
        if($loginInfoRes['status'] != SUCCESS_STATUS){
            return verify_err($loginInfoRes['res'], $loginInfoRes['msg']);
        }

        //获取用户的资金信息  不可在service里的登录方法中验证
//        $fundInfo = FundIndex::FundAccount()->userFund(['user_id'=>$baseInfo['res']['id']]);
//        if($fundInfo['status'] != SUCCESS_STATUS){
//            return $fundInfo;
//        }

        //组装用户信息
//        $allInfo = array_merge($loginInfoRes['res'], $fundInfo['res']);
//        $allInfo && $allInfo['earnings'] = 0;
        return auto_return($loginInfoRes, '成功获取用户信息', '获取用户信息失败');

    }


    //用户审核
    public function passAudit(): array
    {
        $data = pick_array($this->postData, ['name', 'id', 'phone'], false);
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return UserIndex::User()->passAudit($data);
    }

    /*用户分页列表数据*/
    public function list(): array
    {
        $data = pick_array($this->postData, ['page', 'list_rows', 'keywords','authen_status','token'], false);
        return UserIndex::User()->list($data);
    }

    /*更改用户信息*/
    public function edit(): array
    {
        $data = pick_array($this->postData, ['id', 'name', 'nickname','sex','phone','row_ids','row_group_ids','other_info']);
        return UserIndex::User()->edit($data);
    }
    // 用户退出登录
    public function logout(): array
    {
        $data = pick_array($this->postData, ['token']);
        return UserIndex::User()->logout($data);
    }
    // 用户编辑自己的信息
    public function editSelfInfo(): array
    {
        $data = pick_array($this->postData, ['nickname', 'sex', 'token'], false);
        if (array_eq($data, '', ['sex', 'nickname'])) {
            return verify_err($data, 'nickname,sex至少传一个参数');
        }
        return UserIndex::User()->editSelfInfo($data);
    }
    // 用户修改自己的登录密码
    public function editSelfLoginPassword(): array
    {
        // {"login_password":"123456789","old_login_password":"12345678","re_login_password":"123456789"}
        $data = pick_array($this->postData, ['login_password', 'old_login_password', 're_login_password', 'token'], false);
        return UserIndex::User()->editSelfLoginPassword($data);
    }
    // 用户修改自己的支付密码
    public function editSelfPayPassword(): array
    {
        $data = pick_array($this->postData, ['pay_password', 'old_pay_password', 're_pay_password', 'token']);
        return UserIndex::User()->editSelfPayPassword($data);
    }
    // 管理员重置用户登录密码
    public function resetUserLoginPassword(): array
    {
        $data = pick_array($this->postData, ['id', 'name', 'phone', 'login_password', 're_login_password'], false);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return UserIndex::User()->resetUserLoginPassword($data);
    }
    // 管理员重置所有用户登录密码
    public function resetAllLoginPassword(): array
    {
        $data = pick_array($this->postData, ['id', 'name', 'phone', 'login_password', 're_login_password']);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return UserIndex::User()->resetAllLoginPassword($data);
    }
    // 管理员重置用户支付密码
    public function resetUserPayPassword(): array
    {
        $data = pick_array($this->postData, ['id', 'name', 'phone', 'pay_password', 're_pay_password']);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return UserIndex::User()->resetUserPayPassword($data);
    }
    // 管理员重置所有用户支付密码
    public function resetAllPayPassword(): array
    {
        $data = pick_array($this->postData, ['id', 'name', 'phone', 'pay_password', 're_pay_password']);
        if(array_eq($data, '', ['id', 'name', 'phone'])){
            return verify_err(null, 'id,name,phone至少要传一个');
        }
        return UserIndex::User()->resetAllPayPassword($data);
    }

    /*管理员进行审核用户的实名认证信息*/
    public function verifyAuthentication(): array
    {
        $data = pick_array($this->postData, ['id', 'status', 'token']);
        return UserIndex::User()->verifyAuthentication($data);
    }

    /*管理员获取实名认证列表*/
    public function getAuthticationList(): array
    {
        $data = pick_array($this->postData, ['page', 'list_rows', 'keywords','status','token'], false);
        return UserIndex::User()->getAuthticationList($data);
    }
    /**
     * 根据token获取用户权限列表
     */
    public function getAuthByToken ()
    {
        $token = pick_array($this->postData, ['token']);

        return UserIndex::Token()->getAuthByToken($token);
    }

    /**
     * 今周用户注册量(新增用户)/ 总用户
     * @return array
     */
    public function userIncreTendency(): array
    {
        $data = pick_array($this->postData, ['dbDateFormat', 'dateFormat', 'token'], false);

        return UserIndex::User()->userIncreTendency($data);
    }
    /*根据 ID ||用户名 || 手机号 获取某一用户信息*/
    public function info(): array
    {
        $data = pick_array($this->postData, ['id', 'name', 'phone', 'is_add_no_user_counts', 'token'], false);
        if($data['is_add_no_user_counts'] == 1 && !$data['token']){
            return verify_err(null, '请传入参数token');
        }
        if(array_eq($data, '', ['name','id','phone'])){
            return verify_err($data, 'name,id,phone至少要传一个');
        }
        return UserIndex::User()->info($data);
    }
    // 后台添加管理员
    public function addManager()
    {
        $data = pick_array($this->postData,['name','phone', 'real_name','email','department','work_name','role_ids']);
        return UserIndex::User()->addManager($data);
    }

    /**
     * 验证图形验证码
     * @param $data
     * @return mixed
     */
    public function _checkAuthCode($data)
    {
        $checkImg['type'] = $data['img_type'];
        $checkImg['code_id'] = $data['img_code_id'];
        $checkImg['auth_code'] = $data['auth_code'];
        $checkImg['token'] = $data['token'];
//        $checkImg['appkey'] = \app\manage\service\ApiBase::getAppKey();


        return AuthcodeIndex::Authcode()->verifyAuthCode($checkImg);
    }





    // 后台添加客户端用户信息
    public function addClientUser(): array
    {
        //$data = pick_array($this->postData,['name','phone', 'login_password']);
        $data =$this->postData;
        return UserIndex::User()->addClientUser($data);
    }

    //根据id查询客户详情
    public function userClientDetail(){
        $data =$this->postData;
        return UserIndex::User()->userClientDetail($data);
    }

    //根据id编辑客户详情
    public function userEditDetail(){
        $data =$this->postData;
        return UserIndex::User()->userEditDetail($data);
    }

    //领取任务
    public function userGetTask(){
        return UserIndex::User()->userGetTask();
    }

    //用户审核统计
    public function userCheck(){
        return UserIndex::User()->userCheck();
    }

    //根据电话状态返回客户
    public function userClientList(){
        $data =$this->postData;
        return UserIndex::User()->userClientList($data);
    }

    //审核用户
    public function checkClientUser(){
        $data =$this->postData;
        return UserIndex::User()->checkClientUser($data);
    }

    //用户联系时间
    public function userPhoneTime(){
        return UserIndex::User()->userPhoneTime();
    }

    //用户列表
    public function userList(){
        $data = $this->postData;
        return UserIndex::User()->userList($data);
    }
}
